home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Demos / NoiseVBL.c < prev    next >
C/C++ Source or Header  |  1995-04-11  |  8KB  |  238 lines

  1. /*
  2. NoiseVBL.c
  3.  
  4. There are two ways to synchronize your program to a video display. Apple
  5. recommends using the vertical blanking level interrupt (VBL). That approach is
  6. illustrated here. The other approach, which I normally prefer, is to use a side
  7. effect of loading the video cards clut: most video drivers don't return until
  8. the vertical blanking interval. The interrupt approach is more or less
  9. guaranteed to work on all video cards, since Apple more or less requires that it
  10. be supported by all video drivers. Its only disadvantage is that 
  11. interrupts must be enabled. I have the impression--though I haven't collected
  12. hard data--that interrupts steal a lot of time, depending on how busy AppleTalk,
  13. the disk, SCSI, keyboard, mouse, and even miscellaneous INITs that are driven by
  14. VBL interrupts. So when I really want to crank, to display movies, I prefer to
  15. use SetPriority(7) to temporarily suspend all interrupts. In that case the
  16. video-driver side effect approach is your only choice. However, be warned that
  17. some video drivers don't wait, and others take more than a frame to load the
  18. clut, so do some careful timing before you rely on it, i.e. run TimeVideo.
  19.  
  20. This program shows a noise movie, a dynamic random checkerboard, at 66.7 Hz! 
  21. Each frame is displayed by calling CopyWindows.c. After showing each frame once
  22. NoiseVBL cycles through them again. Thus to get a true impression of white noise you
  23. need to compute lots of noise frames, e.g. 100. This may require several
  24. megabytes, depending what your screen's pixel depth is set to.
  25.  
  26. HISTORY:
  27. 11/23/88 Denis Pelli wrote it.
  28.  
  29. 4/20/91 I updated this. It now has essentially no hardware dependencies. It does
  30. require that the video be in a slot, since it uses slot interrupts. A basic
  31. problem with the scheme is that interrupts seem to be shut out while the
  32. interrupt service routines are running. As a result the timing information is
  33. inaccurate since many 1 ms interrupts are lost during the call to
  34. CopyBitsQuickly(). I think the only solution to this would be to put the time
  35. consuming call to CopyBitsQuickly back in the main program, waiting for the
  36. interrupt service routine to give it permission to start each new frame.
  37.  
  38. 4/15/92 Removed all bugs and updated to work with THINK C 5. Now properly set A5
  39. in the interrupt service routines, using the method suggested in Tech Note 180.
  40. I removed the old attempt to disable AppleTalk, which now seems to hang up when
  41. it attempts to reenable AppleTalk. I also removed the clut manipulations.
  42.  
  43. 4/16/92    Put the noise in its own window. Run essentially continuously until
  44. user quits.
  45.  
  46. 8/19/92 Replaced obsolete TimeIt.c by new Timer.c. Timing seems to be fine now.
  47.  
  48. 8/22/92 dgp Moved most of the interrupt service routine code to 
  49. VBLInterruptServiceRoutine.c. Just for fun, I made the program use an alternate monitor,
  50. if available. Fixed a small bug that prevented display of noise on an alternate
  51. monitor if it had a different pixelSize than the main screen.
  52.  
  53. 9/15/92 dgp Updated to use new Timer.c.
  54.  
  55. 2/18/93    dgp    Added fpu test.
  56. 2/20/93    dgp    Call Require(), increased stack space.
  57. 7/7/93    dgp replaced call to CopyBitsQuickly by CopyBits, for compatibility with
  58. Radius PowerView.
  59. 3/19/94    dgp updated the call to Shuffle(), adding new required argument.
  60. 5/30/94    dgp    call StackGrow() before calling NoiseVBL().
  61. 7/27/94 dgp changed "thePort" to &qd.thePort, for broader compatibility, as suggested by
  62. Marty Wachter.
  63. 9/5/94 dgp removed assumption in printf's that int==short.
  64. 11/6/94 dgp updated to demonstrate use of GWorld and CopyWindows.
  65. */
  66.  
  67. #include "VideoToolbox.h"
  68. #include <assert.h>
  69. #if (THINK_C || THINK_CPLUS)
  70.     #include <console.h>
  71. #endif
  72. #if __MWERKS__
  73.     #include <SIOUX.h>
  74. #endif
  75. #if UNIVERSAL_HEADERS
  76.     #include <LowMem.h>
  77. #else
  78.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  79.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  80. #endif
  81.  
  82. /* The user may wish to adjust these. WIDTH should be a multiple of 4. */
  83. #define MAXFRAMES    200        // number of frames in movie
  84. #define WIDTH    256            /* width of displayed noise movie, in pixels */
  85. #define HEIGHT    256            /* height of displayed noise movie, in pixels */
  86. #define RANDOMPHASE 1        /* randomly shift each movie frame */
  87.  
  88. /* This is just for reference. The original is in VideoToolbox.h
  89. struct VBLTaskAndA5 {
  90.     volatile VBLTask vbl;
  91.     long ourA5;
  92.     void (*subroutine)(struct VBLTaskAndA5 *vblData);
  93.     GDHandle device;
  94.     long slot;
  95.     volatile long newFrame;                // Boolean
  96.     volatile long framesLeft;            // count down to zero
  97.     long framesDesired;
  98.     void *ptr;                            // use this for whatever you want
  99. };
  100. typedef struct VBLTaskAndA5 VBLTaskAndA5;
  101. */
  102.  
  103. void main(void);
  104. void NoiseVBL(void);
  105.  
  106. void main()
  107. {
  108.     StackGrow(20000);
  109.     Require(gestalt8BitQD);
  110.     NoiseVBL();
  111. }
  112.  
  113. void NoiseVBL()
  114. {
  115.     int i,error,dx=32,dy=32,dt=1;
  116.     long frames;
  117.     unsigned long seed;
  118.     double s;
  119.     static char string[64];
  120.     Rect r;
  121.     short noiseI=0,noiseN=MAXFRAMES;
  122.     GWorldPtr noiseImage[MAXFRAMES];
  123.     short noiseSequence[MAXFRAMES]; /* shuffled sequence */
  124.     VBLTaskAndA5 noiseVBL;
  125.     WindowPtr window,oldPort;
  126.     EventRecord event;
  127.     Timer *timer;
  128.     int screen;
  129.     ColorTable **ct;
  130.     
  131.     assert(StackSpace()>5000);
  132.     GetDateTime(&seed);
  133.     srand(seed);
  134.     /* INITIALIZE QuickDraw */
  135.     #if (THINK_C || THINK_CPLUS)
  136.         console_options.nrows = 3;
  137.         printf("\n");
  138.     #elif __MWERKS__
  139.         SIOUXSettings.toppixel=LMGetMBarHeight()+19;    // allow for menu bar and title bar
  140.         SIOUXSettings.leftpixel=1;
  141.         SIOUXSettings.rows=3;
  142.         SIOUXSettings.autocloseonquit=0;
  143.         SIOUXSettings.showstatusline=0;
  144.         SIOUXSettings.asktosaveonclose=0;
  145.         printf("\n");
  146.     #else
  147.         InitGraf(&qd.thePort);
  148.         InitFonts();
  149.         InitWindows();
  150.         InitCursor();
  151.     #endif
  152.  
  153.     do{
  154.         if(GetScreenDevice(1)!=NULL)screen=ChooseScreen(1,"Which screen?");
  155.         else screen=0;
  156.         noiseVBL.device=GetScreenDevice(screen);
  157.     }while(noiseVBL.device==NULL);
  158.  
  159.     printf("Enter width of check in pixels (%d)?",dx);
  160.     gets(string);
  161.     sscanf(string,"%d",&dx);
  162.     printf("%d\n",dx);
  163.     printf("Enter height of check in pixels (%d)?",dy);
  164.     gets(string);
  165.     sscanf(string,"%d",&dy);
  166.     printf("%d\n",dy);
  167.     printf("Enter duration of check in frames (%d)?",dt);
  168.     gets(string);
  169.     sscanf(string,"%d",&dt);
  170.     printf("%d\n",dt);
  171.  
  172.     GetPort(&oldPort);
  173.     SetRect(&r,0,0,WIDTH,WIDTH);
  174.     CenterRectInRect(&r,&(*noiseVBL.device)->gdRect);
  175.     OffsetRect(&r,-(r.left%32),0);
  176.     window=NewCWindow(NULL,&r,"\pComputing noise ...",1,noGrowDocProc,(WindowPtr) -1L,0,0L);
  177.     SetPort(window);
  178.     ct=(**(**noiseVBL.device).gdPMap).pmTable;
  179.     for(i=0;i<MAXFRAMES;i++){
  180.         error=NewGWorld(&noiseImage[i],0,&r,ct,noiseVBL.device,keepLocal+noNewDevice+useTempMem);
  181.         if(error)error=NewGWorld(&noiseImage[i],0,&r,ct,noiseVBL.device,keepLocal+noNewDevice);
  182.         if(error)break;
  183.         error=NoiseFill(noiseImage[i],&noiseImage[i]->portRect,dx,dy,RANDOMPHASE);
  184.         LockPixels(noiseImage[i]->portPixMap);
  185.         if(error)break;
  186.     }
  187.     noiseN=i;
  188.     SetPort(oldPort);
  189.     printf("Using %d different frames of noise.\n",(int)noiseN);
  190.     for(i=0;i<noiseN;i++)noiseSequence[i]=i; /* initialize sequence */
  191.     FlushEvents(-1L,0);
  192.     printf("Hit any key to quit.\n");
  193.     SetWTitle(window,"\pNoiseVBL");
  194.     timer=NewTimer();
  195.     do{
  196.         SelectWindow(window);
  197.         Shuffle(noiseSequence,noiseN,sizeof(*noiseSequence));    /* shuffle the images */
  198.         noiseI=0;
  199.         noiseVBL.subroutine=NULL;    // use default
  200.         error=VBLInstall(&noiseVBL,noiseVBL.device,MAXFRAMES);
  201.         if(error)PrintfExit("VBLInstall: error %d\n",error);
  202.         noiseVBL.vbl.vblCount=1;    // Enable interrupt service routine
  203.         while(!noiseVBL.newFrame) ;    // wait for first frame
  204.         noiseVBL.newFrame=0;
  205.         while(!noiseVBL.newFrame) ;    // wait for second frame
  206.         StartTimer(timer);
  207.         frames=noiseVBL.framesLeft;
  208.         while(noiseVBL.framesLeft>1){
  209.             if(noiseVBL.newFrame){
  210.                 noiseVBL.newFrame=0;
  211.                 CopyWindows(noiseImage[noiseSequence[noiseI]],(CWindowPtr)window
  212.                     ,&noiseImage[0]->portRect,&window->portRect,srcCopy,NULL);
  213.                 if(noiseVBL.framesLeft%dt==0){
  214.                     noiseI++;
  215.                     if(noiseI>=noiseN) {
  216.                         Shuffle(noiseSequence,noiseN,sizeof(*noiseSequence));
  217.                         noiseI=0;
  218.                     }
  219.                 }
  220.             }
  221.         }
  222.         while(!noiseVBL.newFrame) ;    // wait for last frame
  223.         s=StopTimer(timer)/1000000.;
  224.         frames-=noiseVBL.framesLeft;
  225.         VBLRemove(&noiseVBL);
  226.         printf("%.1f Hz \r",frames/s);
  227.     }while(!GetNextEvent(keyDown+keyUp+mouseDown,&event));
  228.     FlushEvents(-1L,0);
  229.     for (i=0;i<noiseN;i++) DisposeGWorld(noiseImage[i]);
  230.     DisposeWindow(window);
  231.     DisposeTimer(timer);
  232.     #if (THINK_C || THINK_CPLUS)
  233.         abort();
  234.     #elif __MWERKS__
  235.         SIOUXSettings.autocloseonquit=1;
  236.     #endif
  237. }
  238.